using System.IO;

using HMCollada;
using HMCollada.Core;
using HMCollada.FX;
using System.Xml;

namespace HMCollada {
    internal partial class HMColladaLoader {
        internal static XmlTextReader colladaReader;
    }

    public class HMColladaDocument {
        private const string VERSION = "1.4.1";
       
        private HMColladaAsset asset;
        private HMColladaLibraryEffects library_effects;
        private HMColladaLibraryMaterials library_materials;
        private HMColladaLibraryGeometries library_geometries;
        private HMColladaLibraryVisualScenes library_visual_scenes;
        private HMColladaScene scene;

        public HMColladaDocument(string colladaFile) {
            FileStream stream = new FileStream(colladaFile, FileMode.Open);
            HMColladaLoader.colladaReader = new XmlTextReader(stream);

            // Skip to the COLLADA tag to check the version
            do { HMColladaLoader.colladaReader.Read(); } while (HMColladaLoader.colladaReader.Name != "COLLADA");

            // Make sure we are importing a compatible version
            if (HMColladaLoader.colladaReader.GetAttribute("version") != VERSION)
                throw new XmlException("HMCollada only supports version " + VERSION + " of the COLLADA schema");

            do {
                switch (HMColladaLoader.colladaReader.Name) {
                    case "asset": asset = HMColladaLoader.LoadAsset(); break;
                    case "library_effects": library_effects = HMColladaLoader.LoadLibraryEffects(); break;
                    case "library_materials": library_materials = HMColladaLoader.LoadLibraryMaterials(); break;
                    case "library_geometries": library_geometries = HMColladaLoader.LoadLibraryGeometries(); break;
                    case "library_visual_scenes": library_visual_scenes = HMColladaLoader.LoadLibraryVisualScenes(); break;
                    case "scene": scene = HMColladaLoader.LoadScene(); break;
                    default: break;
                }

                HMColladaLoader.colladaReader.Read();
            } while (HMColladaLoader.colladaReader.Name != "COLLADA"); // loop until the closing COLLADA tag

            HMColladaLoader.colladaReader.Close();
        }
    }
}